package com.lixiaoyao.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lixiaoyao.reggie.entity.Category;
import com.lixiaoyao.reggie.entity.R;
import com.lixiaoyao.reggie.entity.Setmeal;
import com.lixiaoyao.reggie.entity.SetmealDto;
import com.lixiaoyao.reggie.service.CategoryService;
import com.lixiaoyao.reggie.service.SetmealDishService;
import com.lixiaoyao.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());
        setmealService.saveWithSetmealDish(setmealDto);
        return R.success("新增成功");
    }
    /**
     * 套餐分类
     * page?page=1&pageSize=10&name=67
     */
    @CacheEvict(value="setmealCache" , allEntries = true)
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page ,int pageSize,String name){

        Page<Setmeal> setmealPage = new Page<>();
        Page<SetmealDto> setmealDtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage,queryWrapper);

        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> dtoPageRecords = setmealDtoPage.getRecords();
        dtoPageRecords = records.stream().map((ite)->{
            SetmealDto setmealDto = new SetmealDto();
            Long categoryId = ite.getCategoryId();
            Category category = categoryService.getById(categoryId);
            setmealDto.setCategoryName(category.getName());
            BeanUtils.copyProperties(ite,setmealDto);

            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(dtoPageRecords);

        return R.success(setmealDtoPage);

    }

    /**
     * 删除套餐操作
     */
    @CacheEvict(value="setmealCache" , allEntries = true)
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info(ids.toString());
        setmealService.removeWithSetmealDishs(ids);

        return R.success("删除成功");
    }
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> setmealList = setmealService.list(queryWrapper);

        return R.success(setmealList);
    }
   
}
